package com.zw.se2.general;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by zw on 2016/8/13 0013.
 */
public class TopicCleanService implements Job {
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//        DataExpressProxy proxy=DataExpressProxy.getInstance();
//        DataExpressProxy.getInstance().setTimeDifference();
        HashMap<String,Long> map=DataExpressProxy.getTopicsMap();
        Iterator<String> it=map.keySet().iterator();
        Long now=System.currentTimeMillis();
        //检查topicsMap中的topic是否有超时的问题
        while(it.hasNext()){
            String key=  it.next();
            Long topicTime=map.get(key);
            if(isTopicTimeout( now, topicTime)){
                map.remove(key);
                DataExpressProxy.deleteMQTTTopic(key);

            }
        }
        DataExpressProxy.setTopicsMap(map);



    }

    private boolean isTopicTimeout(Long now, Long storedTime) {
        return (now-storedTime)>5*60*1000;
    }

}
